<template>
	<view class="container">
		<unicloud-db ref="udb" v-slot:default="{data, loading, error, options}" :options="options"
			:collection="collectionList" :getone="true" :manual="true">
			<view v-if="error">{{error.message}}</view>
			<view v-else-if="loading">
				<uni-load-more :contentText="loadMore" status="loading"></uni-load-more>
			</view>
			<view v-else-if="data">
				<view>
					<text>活动名称</text>
					<text>{{data.ActivityName}}</text>
				</view>
				<view>
					<text>开始时间</text>
					<uni-dateformat :threshold="[0, 0]" :date="data.ActivityDateTime_start"></uni-dateformat>
				</view>
				<view>
					<text>使用时段</text>
					<text>{{options.use_time_valuetotext[data.use_time]}}</text>
				</view>
				<view>
					<text>使用场地</text>
					<text>{{data.use_position}}</text>
				</view>
				<view>
					<text>使用部门</text>
					<text>{{data.use_department}}</text>
				</view>
				<view>
					<text>受众人数</text>
					<text>{{data.use_people}}</text>
				</view>
				<view>
					<text>负责人</text>
					<text>{{data.user && data.user[0] && data.user[0].text}}</text>
				</view>
				<view>
					<text>负责人电话</text>
					<text>{{data.user_phone}}</text>
				</view>
				<view>
					<text>审核意见</text>
					<text>{{data.shenhe_advice}}</text>
				</view>
				<view>
					<text>审核状态</text>
					<text>{{options.shehe_status_valuetotext[data.shehe_status]}}</text>
				</view>
				<view>
					<text>备注</text>
					<text>{{data.Notes}}</text>
				</view>
			</view>
		</unicloud-db>
		<view class="btns">
			<button type="primary" @click="handleUpdate">修改</button>
			<button type="warn" class="btn-delete" @click="handleDelete">删除</button>
		</view>
	</view>
</template>

<script>
	// 由schema2code生成，包含校验规则和enum静态数据
	import {
		enumConverter
	} from '../../js_sdk/validator/abu_activity_yuyue.js'
	const db = uniCloud.database()

	export default {
		data() {
			return {
				queryWhere: '',
				collectionList: [db.collection('abu_activity_yuyue').field(
					'ActivityName,ActivityDateTime_start,use_time,use_position,use_department,use_people,user,user_phone,shenhe_advice,shehe_status,Notes'
				).getTemp(), db.collection('uni-id-users').field('username as text, _id').getTemp()],
				loadMore: {
					contentdown: '',
					contentrefresh: '',
					contentnomore: ''
				},
				options: {
					// 将scheme enum 属性静态数据中的value转成text
					...enumConverter
				}
			}
		},
		onLoad(e) {
			this._id = e.id
		},
		onReady() {
			if (this._id) {
				this.collectionList = [db.collection('abu_activity_yuyue').where('_id=="' + this._id + '"').field(
					'ActivityName,ActivityDateTime_start,use_time,use_position,use_department,use_people,user,user_phone,shenhe_advice,shehe_status,Notes'
				).getTemp(), db.collection('uni-id-users').field('username as text, _id').getTemp()]
			}
		},
		methods: {
			handleUpdate() {
				// 打开修改页面
				uni.navigateTo({
					url: './edit?id=' + this._id,
					events: {
						// 监听修改页面成功修改数据后, 刷新当前页面数据
						refreshData: () => {
							this.$refs.udb.loadData({
								clear: true
							})
						}
					}
				})
			},
			handleDelete() {
				this.$refs.udb.remove(this._id, {
					success: (res) => {
						// 删除数据成功后跳转到list页面
						uni.navigateTo({
							url: './list'
						})
					}
				})
			}
		}
	}
</script>

<style>
	.container {
		padding: 10px;
	}

	.btns {
		margin-top: 10px;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
	}

	.btns button {
		flex: 1;
	}

	.btn-delete {
		margin-left: 10px;
	}
</style>